import fetch from 'utils/fetch';
import { Toast } from 'vant';

const homeInfo = {
  state: {
    vuexName: 'lzx',
    jokeList: [],
  },
  getters: {
    filterList: (state) => (id) => state.jokeList.filter((item) => item.sid === id),
  },
  mutations: {
    edit(state, payload) {
      state.vuexName = payload.vuexName;
    },
    saveList(state, payload) {
      const { list } = payload;
      state.jokeList = list;
    },
  },
  actions: {
    async getList(context, payload) {
      await fetch('/api/getJoke', payload)
        .then((res) => {
          const { result } = res;
          context.commit('saveList', { list: result });
          Toast.clear();
        })
        .catch((res) => Toast.fail(res.message));
    },
  },
};

export default homeInfo;
